package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasHotel;
import com.kingint.mbg.bas.entity.Scheduling;
import com.kingint.mbg.bas.entity.SchedulingArrangement;
import com.kingint.mbg.bas.entity.UmsClock;
import com.kingint.mbg.bas.mapper.UmsClockMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.SchedulingArrangementService;
import com.kingint.web.admin.base.service.SchedulingService;
import com.kingint.web.admin.base.service.UmsClockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.portal.base.service.impl.HotelServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.DistanceUtils;
import com.kingint.web.util.GetDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户打卡记录关联表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-23
 */
@Service
public class UmsClockServiceImpl extends ServiceImpl<UmsClockMapper, UmsClock> implements UmsClockService {

    @Autowired
    private HotelServiceImpl hotelService;
    @Autowired
    private UmsAdminService umsAdminService;

    @Autowired
    private SchedulingArrangementService schedulingArrangementService;

    @Autowired
    private SchedulingService schedulingService;

    /**
     * 上班打卡
     * @param longitude
     * @param latitude
     * @param currentMember
     * @return
     */
    @Override
    public Object umslocksIn(Double longitude, Double latitude, UmsMember currentMember) {



        Integer hotelId = currentMember.getHotelId();
        if(hotelId==null){
            return K.error().message("此用户没有绑定酒店id");
        }

        Date date = new Date();//获取当前时间
        BasHotel one = hotelService.getById(hotelId);
        Double longitude1 = one.getLongitude();//酒店经度
        Double latitude1 = one.getLatitude();//酒店纬度

        if (Objects.isNull(longitude1) || Objects.isNull(latitude1) ){
            return K.error().message("酒店未设置定位范围");
        }

        QueryWrapper<SchedulingArrangement> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ums_id",currentMember.getId()).eq("hotel_id",currentMember.getHotelId()).eq("scheduling_date",GetDateUtils.getTimeString(GetDateUtils.FORMAT_SHORT)).eq("status",0);
        SchedulingArrangement one2 = schedulingArrangementService.getOne(queryWrapper1);
        if(StringUtils.isEmpty(one2)){
            return K.error().message("该用户今天没有排班，不能打卡");
        }//获取排班情况

        //获取两点之间的距离
        QueryWrapper<UmsClock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ums_id",currentMember.getId()).eq("hotel_id",hotelId).eq("clock_month",date.getMonth()).eq("clock_day",date.getDay());
        UmsClock one1 = this.getOne(queryWrapper);


        // TODO: 2021-01-26 打开逻辑有问题，出现调班怎么办 ,不在时间内这么打卡，一天上班后只能点击下班，e1：超过下班时间


        //计算打卡距离
        Long distance = DistanceUtils.getDistance(longitude, latitude, longitude1, latitude1);
        if(StringUtils.isEmpty(one1)&&currentMember.getWorkStatus().intValue()==0){//
            //上班打卡

            if(distance.longValue()<500){//说明在打卡范围类
                Long id = currentMember.getId();
                UmsMember umsMember = new UmsMember();
                umsMember.setId(currentMember.getId()).setLoginTime(date).setWorkStatus(1);
                umsAdminService.updateById(umsMember);//更新用户状态
                one1=new UmsClock();
                one1.setUmsId(currentMember.getId()).setHotelId(hotelId).setClockMonth(date.getMonth()).setClockDay(date.getDay());
                //记录打卡时间
                one1.setStartTime(date);
                //TODO  打卡排班没做

                // TODO: 2021-01-26 1，拿用户排班的时间节点， 判断打卡时间是否迟到，判断下班时间是否早退
                Date date1 = new Date();
                date1.setHours(8);
                date1.setMinutes(0);
                date1.setSeconds(0);
                long time = date.getTime();//打卡时间
                long time1 = date1.getTime();//规定上班时间
                long time2=time-time1;//迟到为正数

                int late= (int) (time2/1000/60);
                //大于0说明迟到
                one1.setLate(late>0?late:0);
                one1.setAbsent(0);
                one1.setCreateTime(date).setStatus(0);
                boolean save = this.save(one1);
                return K.ok().message("上班打卡成功");

            }else{
                return K.error().message("不在打卡范围内");
            }
        }else{
            //下班打卡
            if(distance.longValue()<500){
                UmsMember umsMember = new UmsMember();
                umsMember.setId(currentMember.getId()).setWorkStatus(0);
                umsAdminService.updateById(umsMember);//更新用户状态//下班
                one1.setEndTime(new Date());
                boolean b = this.updateById(one1);
                return K.ok().message("下班打卡成功");

            }else{
                return K.error().message("不在打卡范围内");
            }

        }


    }

    /**
     * 获取上下班状态
     * @Param workStatus 0下班状态  1上班状态
     * @return
     */
    @Override
    public Object WorkStatus() {
        UmsMember currentMember = umsAdminService.getCurrentMember();
        if(StringUtils.isEmpty(currentMember)){
            return K.error().message("没查到该用户");
        }
        QueryWrapper<SchedulingArrangement> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ums_id",currentMember.getId()).eq("hotel_id",currentMember.getHotelId()).eq("scheduling_date", GetDateUtils.getTimeString(GetDateUtils.FORMAT_SHORT)).eq("status",0);
        SchedulingArrangement one = schedulingArrangementService.getOne(queryWrapper);
        if(StringUtils.isEmpty(one)){
            return K.error().message("该用户今天没有排班，不能打卡");
        }
        one.setScheduling(schedulingService.getById(one.getSchedulingId()));
        Map<String ,Object> map=new HashMap<>();
        map.put("Arrangement",one);//添加排班情况
        Integer workStatus = currentMember.getWorkStatus();//返回是否是上班
        map.put("workStatus",workStatus);//0是下班，还没开始上班，1是已经在上班了，准备开始下班打卡
        map.put("time",new Date());//返回服务器时间
        return K.ok().data(map);
    }
}
